package com.example.taobao

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.newaddress.*
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONArray
import java.lang.Exception
import java.net.URLEncoder
import kotlin.concurrent.thread

class Login : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        supportActionBar?.hide()
        btndenglu.setOnClickListener {
            thread {
                try {
                    val client = OkHttpClient()
                    val requestBody = FormBody.Builder()
                        .add("Zhanghao", URLEncoder.encode(zhanghao.text.toString(),"utf-8"))
                        .add("Mima", URLEncoder.encode(mima.text.toString(),"utf-8"))
                        .build()
                    val request = Request.Builder()
                        .url("http://192.168.43.140:8080/aaa_war_exploded/login.jsp")
                        .post(requestBody)
                        .build()
                    val respone = client.newCall(request).execute()
                    val responeData = respone.body?.string()
                    if (responeData != null) {
                        parseJosn(responeData)
                        finish()
                    }
                    else
                    {
                        Log.d("xx","登录失败")
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }

        btnzhuce.setOnClickListener {
            val intent = Intent()
            intent.setClass(this,Register::class.java)
            startActivity(intent)
        }
    }
    fun tiaoz()
    {
        intent = Intent()
        intent.setClass(this,zhuyem::class.java)
        intent.putExtra("tag",4);
        startActivity(intent)
    }
    private fun parseJosn(jsonStr:String)
    {
        val address :ArrayList<addressinfo> =  ArrayList<addressinfo>()
        try {
            val jaonArray = JSONArray(jsonStr)
            for (i in 0 until jaonArray.length())
            {
                val jsonObject = jaonArray.getJSONObject(i)
                val username = jsonObject.getString("username")
                val userzhanghao = jsonObject.getString("userzhanghao")
                val shouc = jsonObject.getString("shouc")
                val guanz = jsonObject.getString("guanz")
                val zhuj = jsonObject.getString("zhuj")
                val hongb = jsonObject.getString("hongb")
                val editor=getSharedPreferences("data", Context.MODE_PRIVATE)?.edit()
                editor?.putString("name",username)
                editor?.putString("zhangh",userzhanghao)
                editor?.putString("shouc",shouc)
                editor?.putString("guanz",guanz)
                editor?.putString("zuji",zhuj)
                editor?.putString("hongbao",hongb)
                editor?.apply()
            }
        }
        catch (e: Exception)
        {
            e.printStackTrace()
        }
    }
}